#!/bin/bash

# ================================ 快速调试脚本 ================================
# 用于高效测试多模态MCTS系统，避免长时间等待
# 使用最小参数配置，快速发现和解决问题
# 支持并行生成优化，提升推理性能

echo "=========================================="
echo "多模态MCTS快速调试脚本"
echo "=========================================="
echo "时间: $(date)"
echo "目标: 快速测试多模态MCTS基本功能"
echo "=========================================="

# 设置环境变量
export PYTHONPATH="/Users/bwh/Documents/3_code/rStar-1:$PYTHONPATH"

# 创建调试配置文件
DEBUG_CONFIG="configs/debug_multimodal_config.yaml"

cat > "$DEBUG_CONFIG" << 'EOF'
# 快速调试配置文件
model:
  type: "openai_api"
  openai_api:
    base_url: "https://dashscope.aliyuncs.com/compatible-mode/v1"
    api_key: "sk-b112618717a94b4b92f18f665d68e4ed"
    model_name: "qwen2.5-vl-7b-instruct"
    max_concurrent_requests: 4  # 并发请求数，支持并行生成优化
    request_timeout: 60          # 请求超时时间
    max_retries: 2              # 最大重试次数
    
  generation:
    temperature: 0.7
    top_p: 0.9
    max_tokens: 512  # 减少token数量加快响应

data:
  dataset_path: "data/math-vision/test.jsonl"
  image_root: "/Users/bwh/Documents/9_datasets/MathLLMs_MathVision"
  max_samples: 1  # 只测试1个样本
  field_mapping:
    question: "question"
    answer: "answer"
    image: "image"

mcts:
  num_rollouts: 1  # 最小rollout数量
  max_depth_allowed: 2  # 最小深度
  exploration_weight: 1.4
  weight_scheduler: "const"
  discount_factor: 1.0
  num_a1_steps: 2
  num_subquestions: 2
  num_votes: 2
  num_last_votes: 4  # OpenAI API限制最大为4，支持并行生成优化
  disable_a1: false
  disable_a5: true
  enable_potential_score: false  # 禁用潜在分数评估
  modify_prompts_for_rephrasing: false

experiment:
  seed: 42
  note: "debug_test"
  start_idx: 0
  end_idx: 1
  verbose: true
  save_tree: true
  save_intermediate: true
  max_num_worker: 1
  test_batch_size: 1

output:
  results_path: "outputs/debug_results.jsonl"
  outputs_dir: "./run_outputs"
  log_level: "INFO"
  save_answer_sheets: true
  save_solutions: true
  save_statistics: true

multimodal:
  image_format: "base64"
  max_image_size: 1024  # 减小图像尺寸
  always_include_image: true
  image_placeholder: "<image1>"
  show_image_info: true
  log_image_usage: false  # 禁用详细图像日志
EOF

echo "✅ 调试配置文件已创建: $DEBUG_CONFIG"

# 运行调试测试
echo "🚀 开始调试测试..."
python run_src/do_multimodal_generate.py --config "$DEBUG_CONFIG" --verbose

# 检查结果
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
    echo "✅ 调试测试成功完成！"
else
    echo "❌ 调试测试失败，退出码: $EXIT_CODE"
fi

echo "🎉 调试测试完成！"